package com.op.teachingliteapp.database.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.op.teachingliteapp.bean.UserBean;
import com.op.teachingliteapp.database.table.UserTable;
import com.op.teachingliteapp.util.StringUtils;

/**
 * Created by Genji on 2015/6/24. Modified by Kevin
 */
public class UserDao extends AbstractDao {
	public UserDao(Context context) {
		super(context);
		this.tb = UserTable.TABLE_USER;
	}
 
	/**
	 * 获得全部的用户名
	 * 
	 */
	public ArrayList<UserBean> getAllUser() {
		ArrayList<UserBean> userBeanList = new ArrayList<UserBean>();
		Cursor cursor = db.query(this.tb, UserTable.PROJECTION, null, null, null, null, null);
		while (cursor.moveToNext()) {
			userBeanList.add(this.makeUserBeanByCursor(cursor));

		}

		return userBeanList;
	}

	/**
	 * 删除指定用户名的数据
	 * 
	 * @param userNumber
	 */
	public void deleteUserByUserNumber(String userNumber) {
		String whereDelete = UserTable.COLUMN_NUMBER + "=" + '?';
		String[] whereArgsDelete = new String[] { userNumber };
		// 删除符合where子句的行
		db.delete(this.tb, whereDelete, whereArgsDelete);
	}

	/**
	 * 通过用户名来获得该用户的所有信息 
	 * 
	 * @param userNumber
	 * @return
	 */
	public UserBean getUserByUserNumber(String userNumber) {
		UserBean userBean = null;

		Cursor cursor = db.query(this.tb, UserTable.PROJECTION, UserTable.COLUMN_NUMBER + "=" + StringUtils.wrapQuote(userNumber), null, null, null, null);
		while (cursor.moveToNext()) {
			userBean = this.makeUserBeanByCursor(cursor);
		}

		return userBean;
	}
	/**
	 * 通过用户Id来获得该用户的所有信息
	 * 
	 * @param userNumber
	 * @return
	 */
	public UserBean getUserByUserId(String userId) {
		UserBean userBean = null;

		Cursor cursor = db.query(this.tb, UserTable.PROJECTION, UserTable.COLUMN_ID + "=" + StringUtils.wrapQuote(userId), null, null, null, null);
		while (cursor.moveToNext()) {
			userBean = this.makeUserBeanByCursor(cursor);
		}

		return userBean;
	}

	/**
	 * 辅助类，用于生成实例
	 * 
	 * @param cursor
	 * @return
	 */
	private UserBean makeUserBeanByCursor(Cursor cursor) {
		UserBean userBean = new UserBean();
		userBean.setId(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_ID)));
		userBean.setCreateTime(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_CREATE_TIME)));
		userBean.setUpdateTime(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_UPDATE_TIME)));
		userBean.setName(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_NAME)));
		userBean.setPassword(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_PASSWORD)));
		userBean.setEmail(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_EMAIL)));
		userBean.setImageUrl(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_IMAGE_URL)));
		userBean.setNumber(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_NUMBER)));
		userBean.setPhone(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_PHONE)));
		userBean.setIsUsing(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_IS_USING)));
		userBean.setType(cursor.getString(cursor.getColumnIndex(UserTable.COLUMN_TYPE)));
		return userBean;
	}
}
